<?php
/**
 * 活动链接管理
 * huliren<huliren0516@163.com>
 */

define('SITE_AUTH', 'activity-manage-link');
require('../../../common.inc.php');

use lib\core\AdminAction;
use Act\LinkConfig as Model;

class linkconfig extends AdminAction
{
    private $list_url = 'data/modules/manage/linkconfig.php';

    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['Id >' => 0];

        $page = $_GET['page'] < 1 ? 1 : (int)$_GET['page'];
        $limit = parent::PAGE_SIZE;
        $offset = ($page - 1) * $limit;

        $info = Model::getList($where, $offset, $limit);
        $count = $info['count'];
        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $info['data']);
            $smarty->value('url_type', Model::$URL_TYPE);
        }
        $smarty->output('manage/linkconfig_list.tpl');
    }

    public function doEdit()
    {
        $smarty = $this->app->page();
        $id = intval(trim($_GET['id']));

        if ($id < 0) {
            HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
        }
        if (!empty($id)) {
            $data = Model::getDataById($id);
            if (empty($data)) {
                HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
            }

            $smarty->value('info', $data);
        }
        $smarty->value('url_type', Model::$URL_TYPE);
        $smarty->output('manage/linkconfig_edit.tpl');
    }

    public function doUpdata()
    {
        $type = (isset($_POST['type']) and $_POST['type']) ? $_POST['type'] : 0;
        $name = trim($_POST['name']);
        $linkurl = trim($_POST['linkurl']);
        if (stripos($linkurl, 'reqlogin=') !== false && stripos($linkurl, 'qbmeite://') !== 0) {
            HDshowMsg('需要登录的Url必须用协议');
        }

        $data = [];
        $rel = false;
        if (!empty($_POST['id'])) {
            $data = [
                'Type' => $type,
                'Name' => $name,
                'LinkUrl' => $linkurl
            ];

            $rel = Model::update($_POST['id'], $data);
        } else {
            $data = [
                'Type' => $type,
                'Name' => $name,
                'LinkUrl' => $linkurl
            ];

            $rel = Model::add($data);
        }

        if ($rel) {
            HDshowMsg('保存成功', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('操作失败', ['type' => 'error', 'url' => $this->list_url]);
        }
    }

    public function doDelete()
    {
        $id = $_GET['id'];
        $rel = Model::delete($id);
        if ($rel) {
            HDshowMsg('删除成功！', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('删除失败，联系管理员！', ['type' => 'success', 'url' => $this->list_url]);
        }
    }
}

$app->run();